# game constants
from constants import *

# cocos componets used by this scene
from cocos.director import director
from cocos.scene import Scene
from cocos.layer import Layer, ColorLayer
from cocos.scenes.transitions import *
from cocos.actions import *
from cocos.sprite import Sprite
from cocos.text import Label
from cocos.menu import *

# pyglet  componets used by this scene
import pyglet
from pyglet import font
from pyglet.window import key

import sys
import random
from scene_galaxy import *

class PauseLayer(Menu):

    is_event_handler = True     #: enable pyglet's events

    def __init__( self, interface ):
        # call the parent's constructor
        super(PauseLayer, self).__init__("Ingame Menu")

        # keep track of the interface
        self.interface = interface

        # just a uniform color background
        #self.add(ColorLayer(50,50,50,255), z=-1)
        
        self.menu_valign = CENTER
        self.menu_halign = CENTER

        # then add the items
        items = [
            ( MenuItem('Resume Game', director.pop ) ),
            ( MenuItem('New Game', self.on_start_new ) ), 
            ( MenuItem('Load Game', self.on_dummy ) ),
            ( MenuItem('Main Menu', self.on_main_menu ) ),
            ( MenuItem('Options', self.on_dummy ) ),
            ( MenuItem('Credits', self.on_dummy ) ),
            ( MenuItem('Quit', self.on_quit ) ),

        ]

        self.create_menu( items, shake(), shake_back() )
        
             
    def on_start_new(self):
        # empty the scene stack
        for scene in range(len(director.scene_stack)):
            director.pop()

        self.interface.game.newGame(random.randint(0,1000))
        galaxyScene = GalaxyScene(self.interface)
        director.push(galaxyScene)
        
    def on_main_menu(self):
        # empty the scene stack
        for scene in range(len(director.scene_stack)):
            director.pop()
        
    def on_quit(self):
        pyglet.app.exit() 

    def on_dummy(self):
        pass
        
    def on_enter(self):
        super( PauseLayer, self ).on_enter()   

    def on_exit(self):
        super( PauseLayer, self ).on_exit()   

    def on_key_press( self, k , m ):
        #print "Code of pressed key: " + str(k)
        
        if k == key.PAUSE or k == key.ESCAPE:
            director.pop()
            
        return pyglet.event.EVENT_HANDLED
               

# construct the scene here and return
# instead of doing this in the graphic interface (for example self.genericScene = Scene (GenericLayer()) )
# this is because here one can define more complicated scenes, independent of the interface
def IngameMenuScene(interface):
    # in this generic example, build a scene composed by only one layer
    return Scene( PauseLayer(interface) )




